﻿using BLL;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FangChanCrm.Controllers
{
    public class ZhanQuController : Controller
    {
        private readonly It_ZhanQuBiaoService t_ZhanQuBiaoService;

        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;

        public ZhanQuController(It_ZhanQuBiaoService zhanQuBiaoService, It_RenYuanBiaoService renYuanBiaoService, IConfiguration configuration)
        {
            t_ZhanQuBiaoService = zhanQuBiaoService;
            t_RenYuanBiaoService = renYuanBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }



        public IActionResult ZhanQuAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult ZhanQuList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult ZhanQuUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        
        public string Getid() 
        {
            string id = Request.Form["id"];
            t_ZhanQuBiao t_ZhanQuBiao = t_ZhanQuBiaoService.GetZhanQuId(Convert.ToInt32(id));
            string shuju = "{\"ZhanQuMingCheng\":\"" + t_ZhanQuBiao.ZhanQuMingCheng + "\",\"BuMenId\":\"" + t_ZhanQuBiao .BuMenId+ "\"}";
            return shuju;
        }

        public string QiYong() 
        {
            string id = Request.Form["id"];
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var zhanqu = _context.t_ZhanQuBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                zhanqu.ZhuangTai = "启用";
                panduan = _context.SaveChanges() > 0;
            }
            //t_ZhanQuBiao t_ZhanQuBiao = t_ZhanQuBiaoService.GetZhanQuId(Convert.ToInt32(id));
            //t_ZhanQuBiao.ZhuangTai = "启用";
            //bool panduan = t_ZhanQuBiaoService.Update(t_ZhanQuBiao);
            if (panduan)
            {
                return "success";
            }
            else 
            {
                return "err";
            }
        }

        public string JinYong() 
        {
            string id = Request.Form["id"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var zhanqu = _context.t_ZhanQuBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                zhanqu.ZhuangTai = "禁用"; 
                panduan = _context.SaveChanges() > 0;
            }

            //t_ZhanQuBiao t_ZhanQuBiao = t_ZhanQuBiaoService.GetZhanQuId(Convert.ToInt32(id));
            //t_ZhanQuBiao.ZhuangTai = "禁用";
            //bool panduan = t_ZhanQuBiaoService.Update(t_ZhanQuBiao);
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string Update() 
        {
            string id = Request.Form["id"];
            string zhanqumingcheng = Request.Form["zhanqumingcheng"];
            string suoshubumen = Request.Form["suoshubumen"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var zhanqu = _context.t_ZhanQuBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                zhanqu.ZhanQuMingCheng = zhanqumingcheng;
                zhanqu.BuMenId = Convert.ToInt32(suoshubumen);
                panduan = _context.SaveChanges() > 0;
            }

            //t_ZhanQuBiao zhanqu = t_ZhanQuBiaoService.GetZhanQuId(Convert.ToInt32(id));
            //zhanqu.ZhanQuMingCheng = zhanqumingcheng;
            //zhanqu.BuMenId = Convert.ToInt32(suoshubumen);

            //bool panduan = t_ZhanQuBiaoService.Update(zhanqu);

            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string ZhanQuTianJia() {
            string zhanqumingcheng = Request.Form["zhanqumingcheng"];
            string suoshubumen = Request.Form["suoshubumen"];

            t_ZhanQuBiao zhanqu = new t_ZhanQuBiao();
            zhanqu.ZhanQuMingCheng = zhanqumingcheng;
            zhanqu.BuMenId = Convert.ToInt32(suoshubumen);
            zhanqu.ZhuangTai = "启用";

            int panduan = 0;

            panduan = t_ZhanQuBiaoService.Add(zhanqu);

            if (panduan > 0)
            {
                panduan = 1;
            }
            else 
            {
                panduan = 0;
            }
            return panduan.ToString();
        }

        public string GetBuMen() 
        {
            StringBuilder Json = new StringBuilder();
            List<t_BuMenBiao> bumen = t_ZhanQuBiaoService.GetBuMenList();
            Json.Append("[");
            for (int i = 0; i < bumen.Count; i++)
            {
                t_BuMenBiao bum = bumen[i];
                Json.Append("{\"id\":\"" + bum.Id + "\",\"buMenMingCheng\":\"" + bum.BuMenMingCheng + "\"}");
                if ((i + 1) == bumen.Count)
                {

                }
                else {
                    Json.Append(",");
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

        public string GetRenYuan()
        {
            StringBuilder Json = new StringBuilder();
            List<t_RenYuanBiao> renyuan = t_RenYuanBiaoService.GetList();
            Json.Append("[");
            for (int i = 0; i < renyuan.Count; i++)
            {
                t_RenYuanBiao ry = renyuan[i];
                Json.Append("{\"id\":\"" + ry.Id + "\",\"xingMing\":\"" + ry.XingMing + "\",\"lianXiFangShi\":\"" + ry.LianXiFangShi + "\"}");
                if ((i + 1) == renyuan.Count)
                {

                }
                else
                {
                    Json.Append(",");
                }
            }
            Json.Append("]");
            return Json.ToString(); ;
        }

        public string InitializationDataTable()
        {
            t_ZhanQuBiao t_ZhanQuBiao = new t_ZhanQuBiao();
            return t_ZhanQuBiaoService.GetListWithFenYe(t_ZhanQuBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }
    }
}
